package com.chinasoft.controller;

import com.chinasoft.pojo.*;
import com.chinasoft.service.Impl.GoodServiceImpl;
import com.chinasoft.service.Impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/good")
public class GoodController {
    @Autowired
    GoodServiceImpl goodService;
    @PostMapping("/list")
    public List<Good> list() {
        return goodService.findAll();
    }

    @PostMapping("/update")
    public Result updateCount(@RequestBody Good good){
        boolean i = goodService.updateCount(good.getId(), good.getCount());
        if(i){
            return new Result(null, "修改成功");
        }else{
            return Result.fail(500, "修改失败");
        }
    }

    //查询课程类型的总销量
    @PostMapping("/dailnum")
    @ResponseBody
    public Result<GoodCount> getDailSalesByDays() {
        List<GoodCount> result = goodService.findgetNum();
        return new Result(result, "查询成功");
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Good good){
        boolean i = goodService.insert(good);
        if(i){
            return new Result(null, "插入成功");
        }else{
            return Result.fail(500, "插入失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        boolean i = goodService.delete(id);
        if(i){
            return new Result(null, "删除成功");
        }else{
            return Result.fail(500, "删除失败");
        }
    }

    @PostMapping("/updatelist")
    public Result update(@RequestBody Good good){
        boolean i = goodService.update(good);
        if(i){
            return new Result(null, "修改成功");
        } else{
            return Result.fail(500, "修改失败");
        }
    }
}
